const express = require('express');
const jwt = require('jsonwebtoken')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const app = express();

app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())
const miyao = 'zh'
app.post('/dl', (req, res) => {
    //记录用户名
    const user = req.body;
    // console.log(req.body);
    //创建token
    jwt.sign(user, miyao, (err, token) => {
        res.cookie('token', token, {
            maxAge: 1000 * 60 * 60
        });
        res.send('登陆成功')
    });
})
app.get('/ck', (req, res) => {
    console.log(req.cookies);
    jwt.verify(req.cookies.token, miyao, (err, authData) => {
        if (err) {
            res.send('重新登录')
        } else {
            console.log(authData);
            res.send('页面')
        }
    })
})
app.listen(5000, err => {
    if (!err) console.log('启动');
    else console.log(err);
})